package com.hhq.sso.common.exception;

import com.hhq.sso.common.api.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 * Description: 全局异常处理
 * </p>
 *
 * @author : xiaodong.yang
 * @date : 2024/6/20 11:25
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 异常处理
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public CommonResult<String> handle(Exception e) {
        if (e instanceof BusinessException) {
            // 判断异常是否为业务异常
            BusinessException businessException = (BusinessException)e;
            log.error("[BusinessException]---handle error", e);
            return CommonResult.fail(businessException.getCode(), businessException.getMessage());
        } else {
            log.error("[GlobalExceptionHandle]---handle error", e);
            return CommonResult.fail(e.getMessage());
        }
    }
}
